{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori, fpgrowth, association_rules\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'TID': ['T100', 'T200', 'T300', 'T400', 'T500'], 'Items': [{'O', 'N', 'Y', 'E', 'K', 'M'}, {'O', 'N', 'Y', 'D', 'E', 'K'}, {'A', 'E', 'K', 'M'}, {'U', 'Y', 'C', 'K', 'M'}, {'O', 'I', 'E', 'C', 'K'}]}\n"
     ]
    }
   ],
   "source": [
    "# 创建数据集\n",
    "data = {\n",
    "    'TID': ['T100', 'T200', 'T300', 'T400', 'T500'],\n",
    "    'Items': [\n",
    "        {'M', 'O', 'N', 'K', 'E', 'Y'},\n",
    "        {'D', 'O', 'N', 'K', 'E', 'Y'},\n",
    "        {'M', 'A', 'K', 'E'},\n",
    "        {'M', 'U', 'C', 'K', 'Y'},\n",
    "        {'C', 'O', 'K', 'I', 'E'}\n",
    "    ]\n",
    "}\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转换为 DataFrame\n",
    "df = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转换为 One-Hot 编码格式\n",
    "# 将数据集转换为适用于 Apriori 和 FP-Growth 的格式\n",
    "unique_items = sorted(set(item for items in df['Items'] for item in items))\n",
    "encoded_data = pd.DataFrame([{item: (item in items) for item in unique_items} for items in df['Items']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置最小支持度和最小置信度\n",
    "min_support = 0.6  # 60%\n",
    "min_confidence = 0.8  # 80%\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "频繁项集 (Apriori):\n",
      "    support   itemsets\n",
      "0       0.8        (E)\n",
      "1       1.0        (K)\n",
      "2       0.6        (M)\n",
      "3       0.6        (O)\n",
      "4       0.6        (Y)\n",
      "5       0.8     (E, K)\n",
      "6       0.6     (O, E)\n",
      "7       0.6     (K, M)\n",
      "8       0.6     (O, K)\n",
      "9       0.6     (K, Y)\n",
      "10      0.6  (O, E, K)\n"
     ]
    }
   ],
   "source": [
    "# 使用 Apriori 算法\n",
    "frequent_item_sets_apriori = apriori(encoded_data, min_support=min_support, use_colnames=True)\n",
    "# 输出频繁项集\n",
    "print(\"频繁项集 (Apriori):\")\n",
    "print(frequent_item_sets_apriori)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "频繁项集 (FP-growth):\n",
      "    support   itemsets\n",
      "0       1.0        (K)\n",
      "1       0.8        (E)\n",
      "2       0.6        (Y)\n",
      "3       0.6        (O)\n",
      "4       0.6        (M)\n",
      "5       0.8     (E, K)\n",
      "6       0.6     (K, Y)\n",
      "7       0.6     (O, E)\n",
      "8       0.6     (O, K)\n",
      "9       0.6  (O, E, K)\n",
      "10      0.6     (K, M)\n"
     ]
    }
   ],
   "source": [
    "# 使用 FP-growth 算法\n",
    "frequent_item_sets_fpgrowth = fpgrowth(encoded_data, min_support=min_support, use_colnames=True)\n",
    "# 输出频繁项集\n",
    "print(\"\\n频繁项集 (FP-growth):\")\n",
    "print(frequent_item_sets_fpgrowth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成关联规则\n",
    "rules_apriori = association_rules(frequent_item_sets_apriori, metric=\"confidence\", min_threshold=min_confidence)\n",
    "rules_fpgrowth = association_rules(frequent_item_sets_fpgrowth, metric=\"confidence\", min_threshold=min_confidence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "关联规则 (Apriori):\n",
      "  antecedents consequents  support  confidence\n",
      "0         (E)         (K)      0.8         1.0\n",
      "1         (K)         (E)      0.8         0.8\n",
      "2         (O)         (E)      0.6         1.0\n",
      "3         (M)         (K)      0.6         1.0\n",
      "4         (O)         (K)      0.6         1.0\n",
      "5         (Y)         (K)      0.6         1.0\n",
      "6      (O, E)         (K)      0.6         1.0\n",
      "7      (O, K)         (E)      0.6         1.0\n",
      "8         (O)      (E, K)      0.6         1.0\n",
      "\n",
      "关联规则 (FP-growth):\n",
      "  antecedents consequents  support  confidence\n",
      "0         (E)         (K)      0.8         1.0\n",
      "1         (K)         (E)      0.8         0.8\n",
      "2         (Y)         (K)      0.6         1.0\n",
      "3         (O)         (E)      0.6         1.0\n",
      "4         (O)         (K)      0.6         1.0\n",
      "5      (O, E)         (K)      0.6         1.0\n",
      "6      (O, K)         (E)      0.6         1.0\n",
      "7         (O)      (E, K)      0.6         1.0\n",
      "8         (M)         (K)      0.6         1.0\n"
     ]
    }
   ],
   "source": [
    "# 和关联规则\n",
    "print(\"\\n关联规则 (Apriori):\")\n",
    "print(rules_apriori[['antecedents', 'consequents', 'support', 'confidence']])\n",
    "print(\"\\n关联规则 (FP-growth):\")\n",
    "print(rules_fpgrowth[['antecedents', 'consequents', 'support', 'confidence']])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
